Oa.controller('fileCtrl',function($scope,$http,$stateParams){

  // switch ($stateParams.type) {
  //   case 0:break;
  //   case 1:break;
  //   case 2:break;
  //   case 3:break;
  // }

  //获取文件
  $scope.totalPage = 1;//全部页数
  $scope.currentPage = 1;//当前页码
  $scope.pageArr;// 页码数组
  $scope.size = 20;
  // 生成页码数组
  $scope.getPage = function(num){
    $scope.pageArr = new Array();
    for(var i=0;i<num;i++){
      $scope.pageArr[i] = i+1;
    }
  }
  //获取   第n页的订单记录
  $scope.getFiles = function(num){
    if(num>=1 && num<=$scope.totalPage){
        $http.get(getFiles_url,
          {
            params:{
              page:num,
              size:$scope.size,
              type:$stateParams.type
            }
          })
        .success(function(res){
          if (res.code==200) {
            $scope.files = res.data.list;
            $scope.totalPage = res.data.countPage;
            $scope.currentPage = num;
            $scope.getPage($scope.totalPage);
            $scope.recordsnum = res.data.recordsNum;
            for (var i = 0; i < $scope.files.length; i++) {
              $scope.files[i].date = new Date($scope.files[i].uploadDate).toLocaleString();
              $scope.files[i].fileUrl = "http://localhost:8080/"+ $scope.files[i].fileUrl;
            }
          }else {
            Notify(res.data,"top-right");
          }
        }).error(function(){
          Notify('客户端错误',"top-right");
      });
    }
  }
  $scope.getRecord = function(){
    $scope.getFiles($scope.currentPage);
  }
  $scope.getRecord();

  $scope.reverseFiles = function(){
    $scope.files.reverse();
  }

  //删除文件
  $scope.showModal = false;
  $scope.filecheckbox;
  $scope.delFiles = function() {
    $scope.showModal = !$scope.showModal;
  };
  $scope.sureDelFiles = function() {
    var arr=[];
    var getSelectValueFiles = $("input[name='checkbox']:checked").each(function(j) {
        if (j >= 0) {
          arr.push($(this).val());
        }
    });
    $http.post(delFile_url,{
    	filesIds: arr
      })
      .success(function(res) {
    	$scope.getRecord();
        Notify(res.msg,"top-right");
      }).error(function() {
        Notify("客户端错误","top-right");
    });
    $scope.showModal = !$scope.showModal;
  }

  //通过文件名查询文件
  $scope.findFilesByName = function(id){
    if(id!=null && id!='' && id!=' '){
      $http.get(findFileByName_url,
        {
          params:{
            filesName:id,
            type:$stateParams.type
          }
        })
        .success(function(res){
          if (res.code==200) {
            $scope.files = res.data;
            $scope.totalPage = 1;
            $scope.currentPage = 1;
            $scope.recordsnum = res.data.length;
            $scope.getPage(1);
            for (var i = 0; i < $scope.files.length; i++) {
                $scope.files[i].date = new Date($scope.files[i].uploadDate).toLocaleString();
                $scope.files[i].fileUrl = "http://localhost:8080/"+ $scope.files[i].fileUrl;
              }
          }else {
            Notify(res.msg,"top-right");
          }
        }).error(function(){
          Notify('客户端错误',"top-right");
      });
    }else{
    	$scope.getRecord();
    }
  }
  $scope.keyupId = function(){
    if(window.event.keyCode == 13){
      $scope.findFilesByName($scope.checkname);
    }
  }


  //上传文件
  $scope.showUploadModal = false;
  $scope.uploadFiles = function() {
    $scope.showUploadModal = !$scope.showUploadModal;
  };
  $scope.sureUploadFiles = function() {
    var formData = new FormData($( "#uploadFilesForm" )[0]);
    formData.append("type",$scope.newfile.type);
    var xhr = new XMLHttpRequest();
    xhr.onload=function(event)
    {
      if  (( xhr.status >= 200 && xhr.status < 300) || xhr.status == 304)   //上传成功
      {
        var res = JSON.parse(event.target.response);
        $scope.getRecord();
        Notify(res.msg,"top-right");
      }
      else
      {
        Notify("上传文件失败","top-right");
      }
    };

    xhr.open("post", uploadFiles_url ,true);
    xhr.send(formData);
    $scope.showUploadModal = !$scope.showUploadModal;
  }
  $scope.newfile = {
    type:0,
    title:null
  };
  $scope.selectfile = function(file){
    $scope.newfile.title = file[0].name;
    $("input[type='file']").blur();
    // $("input[name='filesName']").focus();
    // $("input[name='filesName']").blur();

    var index = file[0].name.lastIndexOf('.');
    var typeStr = file[0].name.substring(index);
    var picture = [".bmp",".jpg",".jpeg",".png",".gif"];
    var video = [".mp4","3gp",".mov",".m4v",".avi",".dat",".mkv",".flv",".vod",".rm",".rmvb",".asx",".asf",".wmv"];
    if (picture.indexOf(typeStr)!=-1) {$scope.newfile.type=1;console.log(typeStr,1);}
    else if (video.indexOf(typeStr)!=-1) {$scope.newfile.type=3;console.log(typeStr,3);}
    else{$scope.newfile.type=2;console.log(typeStr,2);}
  }
});
